package com.nanhai.buddhist.college.module.study.service.subjectconfig;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.nanhai.buddhist.college.module.study.controller.admin.subjectconfig.vo.*;
import com.nanhai.buddhist.college.module.study.dal.dataobject.subjectconfig.StudySubjectConfigDO;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;

import com.nanhai.buddhist.college.module.study.convert.subjectconfig.StudySubjectConfigConvert;
import com.nanhai.buddhist.college.module.study.dal.mysql.subjectconfig.StudySubjectConfigMapper;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.study.enums.ErrorCodeConstants.*;

/**
 * 课程管理-配置 Service 实现类
 *
 * @author xqenzo@163.com
 */
@Service
@Validated
public class StudySubjectConfigServiceImpl implements StudySubjectConfigService {

    @Resource
    private StudySubjectConfigMapper subjectConfigMapper;

    @Override
    public Long createSubjectConfig(StudySubjectConfigCreateReqVO createReqVO) {
        // 插入
        StudySubjectConfigDO subjectConfig = StudySubjectConfigConvert.INSTANCE.convert(createReqVO);
        subjectConfigMapper.insert(subjectConfig);
        // 返回
        return subjectConfig.getId();
    }

    @Override
    public void updateSubjectConfig(StudySubjectConfigUpdateReqVO updateReqVO) {


        StudySubjectConfigDO studySubjectConfigDO = getSubjectConfigBySubjectId(updateReqVO.getStudySubjectId());
        if (Objects.isNull(studySubjectConfigDO)) {
            StudySubjectConfigDO subjectConfig = StudySubjectConfigConvert.INSTANCE.convert(updateReqVO);
            subjectConfigMapper.insert(subjectConfig);
        } else {
            StudySubjectConfigDO updateObj = StudySubjectConfigConvert.INSTANCE.convert(updateReqVO);
            LambdaUpdateWrapper<StudySubjectConfigDO> objectLambdaUpdateWrapper = Wrappers.lambdaUpdate();
            objectLambdaUpdateWrapper.eq(StudySubjectConfigDO::getStudySubjectId, updateReqVO.getStudySubjectId());
            subjectConfigMapper.update(updateObj, objectLambdaUpdateWrapper);
        }


    }

    @Override
    public void deleteSubjectConfig(Long id) {
        // 校验存在
        this.validateSubjectConfigExists(id);
        // 删除
        subjectConfigMapper.deleteById(id);
    }

    private void validateSubjectConfigExists(Long id) {
        if (subjectConfigMapper.selectById(id) == null) {
            throw exception(SUBJECT_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public StudySubjectConfigDO getSubjectConfig(Long id) {
        return subjectConfigMapper.selectById(id);
    }

    @Override
    public StudySubjectConfigDO getSubjectConfigBySubjectId(String subjectId) {
        return subjectConfigMapper.selectOne(Wrappers.lambdaQuery(StudySubjectConfigDO.builder().studySubjectId(subjectId).build()));
    }

    @Override
    public List<StudySubjectConfigDO> getSubjectConfigList(Collection<Long> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : subjectConfigMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<StudySubjectConfigDO> getSubjectConfigPage(StudySubjectConfigPageReqVO pageReqVO) {
        return subjectConfigMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StudySubjectConfigDO> getSubjectConfigList(StudySubjectConfigExportReqVO exportReqVO) {
        return subjectConfigMapper.selectList(exportReqVO);
    }

}
